﻿namespace ManagerClinic.CanLamSang
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Linq;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using System.Configuration;
    using ManagerClinic.Ultilites.HtmlEditor.Buttons;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmPhieuPhauThuatThuThuat : DevExpress.XtraEditors.XtraForm
    {

        string _MaPhieuPhauThuatThuThuat = string.Empty, _MaHoSoKhamBenh = string.Empty, _MaDichVu = string.Empty;
        Int32 _status;
        bool resultInsert = false;
        public event EventHandler _SavePhieuCanLamSang = null;

        HoSoKhamBenhNgoaiTruEntity hoSoKhamBenhNgoaiTruEntity = null;
        PhieuPhauThuatThuThuatDiChuyenEntity phieuPhauThuatThuThuatDiChuyenEntity = null;

        HoSoKhamBenhNgoaiTruBusiness hoSoKhamBenhNgoaiTruBusiness = new HoSoKhamBenhNgoaiTruBusiness();
        ThuPhiDichVuBusiness thuPhiDichVuBusiness = new ThuPhiDichVuBusiness();
        DmBacSyBusiness dmBacSyBusiness = new DmBacSyBusiness();
        PhieuPhauThuatThuThuatDiChuyenBusiness phieuPhauThuatThuThuatDiChuyenBusiness = new PhieuPhauThuatThuThuatDiChuyenBusiness();
        PhieuPhauThuatThuThuatDiChuyenChiTietBusiness phieuPhauThuatThuThuatDiChuyenChiTietBusiness = new PhieuPhauThuatThuThuatDiChuyenChiTietBusiness();

        public frmPhieuPhauThuatThuThuat(Int32 status, string maDichVu, HoSoKhamBenhNgoaiTruEntity obj)
        {
            InitializeComponent();

            PredefinedButtonSets.SetupDefaultButtons(this.txtKetQua);
            PredefinedButtonSets.SetupDefaultButtons(this.txtKetLuan);

            _status = status;
            resultInsert = false;
            hoSoKhamBenhNgoaiTruEntity = obj;
            _MaDichVu = maDichVu;
            _MaPhieuPhauThuatThuThuat = ConstantManagerClinic.setCodeManagerClinic(ConstantManagerClinic.maPhieuPhauThuatThuThuat);

            ucThongTinHanhChinh.LoadThongTinHanhChinh(hoSoKhamBenhNgoaiTruEntity.MaBenhNhan);
            ucChiDinhKhoaPhong.LoadDmKhoaKham();
            ucChiDinhKhoaPhong.LoadDmKhoaCanLamSang();
            ucChiDinhKhoaPhong.LoadDmPhongKham();
            ucChiDinhKhoaPhong.LoadDmPhongCanLamSang();
            ucChiDinhKhoaPhong.LoadDmDichVuChiTiet(_MaDichVu);

        }

        public frmPhieuPhauThuatThuThuat(Int32 status, string maPhieuPhauThuat)
        {
            InitializeComponent();

            PredefinedButtonSets.SetupDefaultButtons(this.txtKetQua);
            PredefinedButtonSets.SetupDefaultButtons(this.txtKetLuan);

            _status = status;
            resultInsert = false;
            _MaPhieuPhauThuatThuThuat = maPhieuPhauThuat;
            phieuPhauThuatThuThuatDiChuyenEntity = phieuPhauThuatThuThuatDiChuyenBusiness.GetPhieuPhauThuatThuThuatDiChuyen(_MaPhieuPhauThuatThuThuat);
            hoSoKhamBenhNgoaiTruEntity = hoSoKhamBenhNgoaiTruBusiness.GetHoSoKhamBenhNgoaiTru(phieuPhauThuatThuThuatDiChuyenEntity.MaHoSo);
            _MaDichVu = phieuPhauThuatThuThuatDiChuyenEntity.MaDichVu;

            ucThongTinHanhChinh.LoadThongTinHanhChinh(phieuPhauThuatThuThuatDiChuyenEntity.MaBenhNhan);
            ucChiDinhKhoaPhong.LoadDmKhoaKham();
            ucChiDinhKhoaPhong.LoadDmKhoaCanLamSang();
            ucChiDinhKhoaPhong.LoadDmPhongKham();
            ucChiDinhKhoaPhong.LoadDmPhongCanLamSang();
            ucChiDinhKhoaPhong.setMaKhoaKham(phieuPhauThuatThuThuatDiChuyenEntity.MaKhoaKham);
            ucChiDinhKhoaPhong.setMaPhongKhamBenh(phieuPhauThuatThuThuatDiChuyenEntity.MaPhongKham);
            ucChiDinhKhoaPhong.setMaKhoaCanLamSang(phieuPhauThuatThuThuatDiChuyenEntity.MaKhoaCanLamSang);
            ucChiDinhKhoaPhong.setMaPhongCanLamSang(phieuPhauThuatThuThuatDiChuyenEntity.MaPhongCanLamSang);
            ucChiDinhKhoaPhong.LoadDmDichVuChiTiet(_MaDichVu);
        }

        void LoadInfoPhieu(PhieuPhauThuatThuThuatDiChuyenEntity obj)
        {
            ucChiDinhKhoaPhong.setMaKhoaKham(obj.MaKhoaKham);
            ucChiDinhKhoaPhong.setMaPhongKhamBenh(obj.MaPhongKham);
            ucChiDinhKhoaPhong.setMaKhoaCanLamSang(obj.MaKhoaCanLamSang);
            ucChiDinhKhoaPhong.setMaPhongCanLamSang(obj.MaPhongCanLamSang);
            ucChiDinhKhoaPhong.setBacSy(obj.MaBacSyKham);

            txtKetLuan.Html = obj.KetLuan;
            txtKetQua.Html = obj.KetQua;

            var dmBacSyClsEntity = dmBacSyBusiness.GetDmBacSy(obj.MaBacSyCls);
            if (dmBacSyClsEntity != null)
                txtBacSyCls.Text = dmBacSyClsEntity.MaBacSy + " - " + dmBacSyClsEntity.TenBacSy;
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            ucChiDinhKhoaPhong.IsValidLookupEdit();
            ucChiDinhKhoaPhong.IsValidChooseDichVuChiTiet();

            if (resultInsert == false)
            {
                phieuPhauThuatThuThuatDiChuyenEntity = new PhieuPhauThuatThuThuatDiChuyenEntity()
                {
                    MaPhauThuatThuThuat = _MaPhieuPhauThuatThuThuat.Trim(),
                    Stt = 0,
                    SoVaoVien = hoSoKhamBenhNgoaiTruEntity.SoVaoVien,
                    MaHoSo = hoSoKhamBenhNgoaiTruEntity.MaHoSo,
                    NgayVaoKham = hoSoKhamBenhNgoaiTruEntity.NgayVaoKham,
                    MaKhoaCanLamSang = ucChiDinhKhoaPhong.getMaKhoaCanLamSang(),
                    MaPhongCanLamSang = ucChiDinhKhoaPhong.getMaPhongCanLamSang(),
                    MaKhoaKham = ucChiDinhKhoaPhong.getMaKhoaKham(),
                    MaPhongKham = ucChiDinhKhoaPhong.getMaPhongKham(),
                    MaDichVu = _MaDichVu,
                    KetLuan = txtKetLuan.Html.Trim(),
                    KetQua = txtKetQua.Html.Trim(),
                    GhiChu = ucChiDinhKhoaPhong.getGhiChu(),
                    YeuCau = ucChiDinhKhoaPhong.getYeuCau(),
                    TrangThaiPhieu = (int)ConstantManagerClinic.TrangThaiPhieuCanLamSang.TaoMoi,
                    MaBacSyCls = string.Empty,
                    MaBacSyKham = Authenticate.MaBacSy,
                    TrangThaiCho = Convert.ToBoolean(ConstantManagerClinic.TrangThaiChoDichVuKhamChuaBenh.ChuaThucHien)
                };
                resultInsert = phieuPhauThuatThuThuatDiChuyenBusiness.InsertPhieuPhauThuatThuThuatDiChuyen(phieuPhauThuatThuThuatDiChuyenEntity);
            }
            else
            {
                phieuPhauThuatThuThuatDiChuyenEntity = phieuPhauThuatThuThuatDiChuyenBusiness.GetPhieuPhauThuatThuThuatDiChuyen(_MaPhieuPhauThuatThuThuat);
                phieuPhauThuatThuThuatDiChuyenEntity.YeuCau = ucChiDinhKhoaPhong.getYeuCau();
                phieuPhauThuatThuThuatDiChuyenEntity.GhiChu = ucChiDinhKhoaPhong.getGhiChu();
                phieuPhauThuatThuThuatDiChuyenEntity.KetQua = txtKetQua.Html.Trim();
                phieuPhauThuatThuThuatDiChuyenEntity.KetLuan = txtKetLuan.Html.Trim();
                phieuPhauThuatThuThuatDiChuyenEntity.MaKhoaCanLamSang = ucChiDinhKhoaPhong.getMaKhoaCanLamSang();
                phieuPhauThuatThuThuatDiChuyenEntity.MaPhongCanLamSang = ucChiDinhKhoaPhong.getMaPhongCanLamSang();
                phieuPhauThuatThuThuatDiChuyenEntity.MaDichVu = _MaDichVu;
                phieuPhauThuatThuThuatDiChuyenEntity.MaKhoaKham = ucChiDinhKhoaPhong.getMaKhoaKham();
                phieuPhauThuatThuThuatDiChuyenEntity.MaPhongKham = ucChiDinhKhoaPhong.getMaPhongKham();

                phieuPhauThuatThuThuatDiChuyenBusiness.EditPhieuPhauThuatThuThuatDiChuyen(phieuPhauThuatThuThuatDiChuyenEntity);
            }
            //delete from table ThuPhiDichVu
            thuPhiDichVuBusiness.DeleteThuPhiDichVuByMaPhieuDichVu(_MaPhieuPhauThuatThuThuat);
            //delete from table PhieuPhauThuatThuThuatChiTiet
            phieuPhauThuatThuThuatDiChuyenChiTietBusiness.DeletePhieuPhauThuatThuThuatDiChuyenChiTiet_ByMaPhauThuatThuThuat(_MaPhieuPhauThuatThuThuat);

            if (resultInsert == true)
            {
                IList<DmDichVuChiTietEntity> lstDmDichVuChiTietIsChecked = ucChiDinhKhoaPhong.DmDichVuChiTietEntityIsChecked;
                if (lstDmDichVuChiTietIsChecked == null) { return; }

                foreach (DmDichVuChiTietEntity dmDichVuChiTietEntity in lstDmDichVuChiTietIsChecked)
                {
                    var thuphiDichVuEntity = new ThuPhiDichVuEntity()
                    {
                        MaPhieuDichVu = _MaPhieuPhauThuatThuThuat,
                        MaHoSo = phieuPhauThuatThuThuatDiChuyenEntity.MaHoSo,
                        NgayDenKham = phieuPhauThuatThuThuatDiChuyenEntity.NgayVaoKham,
                        MaDichVu = _MaDichVu,
                        MaChiTieu = dmDichVuChiTietEntity.MaChiTieu,
                        TongTien = dmDichVuChiTietEntity.DonGia,
                        DaDongTien = (int)ConstantManagerClinic.TrangThaiDongTien.ChuaDongTien,
                        MaNguoiDung = Authenticate.UserName,
                        MaKhoaKham = phieuPhauThuatThuThuatDiChuyenEntity.MaKhoaKham,
                        MaPhongKham = phieuPhauThuatThuThuatDiChuyenEntity.MaPhongKham,
                        MaKhoaCanLamSang = phieuPhauThuatThuThuatDiChuyenEntity.MaKhoaCanLamSang,
                        MaPhongCanLamSang = phieuPhauThuatThuThuatDiChuyenEntity.MaPhongCanLamSang,
                        DaThucHien = Convert.ToBoolean(ConstantManagerClinic.TrangThaiThucHienDichVuKhamChuaBenh.ChuaThucHien)
                    };
                    thuPhiDichVuBusiness.InsertThuPhiDichVu(thuphiDichVuEntity);

                    var phieuPhauThuatThuThuatDiChuyenChiTietEntity = new PhieuPhauThuatThuThuatDiChuyenChiTietEntity()
                    {
                        MaPhauThuatThuThuat = _MaPhieuPhauThuatThuThuat,
                        MaChiTieu = dmDichVuChiTietEntity.MaChiTieu,
                        DonGia = dmDichVuChiTietEntity.DonGia,
                        DaThuTien = (int)ConstantManagerClinic.TrangThaiDongTien.ChuaDongTien
                    };
                    phieuPhauThuatThuThuatDiChuyenChiTietBusiness.InsertPhieuPhauThuatThuThuatDiChuyenChiTiet(phieuPhauThuatThuThuatDiChuyenChiTietEntity);
                }
                MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (_SavePhieuCanLamSang != null) _SavePhieuCanLamSang(sender, e);
        }

        private void btnDongKetQua_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuuKetQua_Click(object sender, EventArgs e)
        {
            phieuPhauThuatThuThuatDiChuyenEntity = phieuPhauThuatThuThuatDiChuyenBusiness.GetPhieuPhauThuatThuThuatDiChuyen(_MaPhieuPhauThuatThuThuat);
            phieuPhauThuatThuThuatDiChuyenEntity.KetQua = txtKetQua.Html;
            phieuPhauThuatThuThuatDiChuyenEntity.KetLuan = txtKetLuan.Html;
            phieuPhauThuatThuThuatDiChuyenEntity.MaBacSyCls = Authenticate.MaBacSy;
            phieuPhauThuatThuThuatDiChuyenBusiness.EditPhieuPhauThuatThuThuatDiChuyen(phieuPhauThuatThuThuatDiChuyenEntity);
            MessageBox.Show("Cập nhật dữ liệu thành công!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
        }

        private void frmPhieuPhauThuatThuThuat_Load(object sender, EventArgs e)
        {
            ucChiDinhKhoaPhong.setDmDichVuChiTietEntityIsChecked(_MaPhieuPhauThuatThuThuat, _MaDichVu);
        }


    }
}